package com.linhai.mybatis.controller;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.linhai.mybatis.model.User;
import com.linhai.mybatis.model.EventLog;
import com.linhai.mybatis.mvc.annotation.AuthPassport;
import com.linhai.mybatis.service.IUserService;
import com.linhai.mybatis.service.IEventLogService;
import com.linhai.mybatis.webmodel.UploadForm;

@Controller
@RequestMapping("/")
public class UserController {

	@Resource
	private IUserService service;
	
	@Resource
	private IEventLogService eventLogService;

	//@AuthPassport//权限验证拦截器
	@RequestMapping("index")
	public String index(ModelMap model) {// 方法里的参数可以用来把对象或值传递给前台
		java.util.List<com.linhai.mybatis.model.User> users = service
				.findUsersByName("%");
		model.put("users", users);// 前台可以直接取${users}
		// users=service.findUsersByName("%");
		System.out.println("执行首页跳转的方法");
		for (User user : users) {
			System.out.println(user.getId() + ":" + user.getUserName() + ":"
					+ user.getUserAddress());
		}
		
		System.out.println("读取事件日志！");
		java.util.List<EventLog> eventLogs=eventLogService.findEventLogsByEventName("%");
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(EventLog eventLog:eventLogs){
			System.out.println(eventLog.getId() + ":" + sdf.format(eventLog.getEventDateTime())
					+ eventLog.getEventName() + ":" + eventLog.getEventType());
		}
		System.out.println("读取事件日志结束");
		return "index";
	}

	@RequestMapping("upform")
	public String upform() {
		return "upform";
	}

	@RequestMapping("upload")
	public String upload(ModelMap model, UploadForm upform) {
		try {
			MultipartFile uploadFile = upform.getFile();
			String fileName = uploadFile.getOriginalFilename();
			java.io.InputStream is = uploadFile.getInputStream();
			java.io.File tmpFile = new java.io.File("/tmp_upload/" + fileName);
			if (tmpFile.exists()) {
				boolean delFileResult = tmpFile.delete();
				System.out.println("删除己存在的文件：" + delFileResult);
			} else {
				throw new Exception(tmpFile.getAbsolutePath());
			}
			if (!fileName.equals("")) {
				java.io.FileOutputStream fos = new java.io.FileOutputStream(
						tmpFile);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				fos.close();
				is.close();
				model.put("message", "上传成功" + tmpFile.getAbsolutePath());
			}
		} catch (java.io.FileNotFoundException fe) {
			model.put("message", fe.getMessage());
			System.out.println("找不到文件的异常：" + fe.getMessage());
			fe.printStackTrace();
		} catch (java.io.IOException ie) {
			model.put("message", ie.getMessage());
			System.out.println("IO异常：" + ie.getMessage());
			ie.printStackTrace();
		} catch (Exception e) {
			model.put("message", e.getMessage());
			System.out.println("未知异常：" + e.getMessage());
			e.printStackTrace();
		}
		return "index";
	}

	@ResponseBody
	@RequestMapping("testRequest")
	// @RequestMapping(value = "/testRequest", produces =
	// "text/html;charset=UTF-8")
	public String testRequest(com.linhai.mybatis.model.User user) {
		System.out.println("接收mode对象");
		System.out.println("userName==" + user.getUserName());
		System.out.println("userAddress==" + user.getUserAddress());
		return "{msg:\"提交成功\"}";
	}

	@ResponseBody
	@RequestMapping("getJson")
	public String listJson() {
		System.out.println("getJson");
		// java.util.List<com.linhai.mybatis.model.User>
		// users=service.findUsersByName("%");
		return "[{\"userName\":\"张三\",\"password\":\"zhangshang\"},{\"userName\":\"李四\",\"password\":\"lishi\"}]";
	}

}